﻿/// <reference path="adapter/ext/ext-base-debug.js" />

/// <reference path="ext-all-debug-w-comments.js" />
Ext.util.TaskRunner = function (interval) {
    interval = interval || 10;
    var tasks = [],
        removeQueue = [],
        id = 0,
        running = false,

        // private
        stopThread = function () {
            running = false;
            clearInterval(id);
            id = 0;
        },

        // private
        startThread = function () {
            if (!running) {
                running = true;
                id = setInterval(runTasks, interval);
            }
        },

        // private
        removeTask = function (t) {
            removeQueue.push(t);
            if (t.onStop) {
                t.onStop.apply(t.scope || t);
            }
        },

        // private
        runTasks = function () {
            var rqLen = removeQueue.length,
                        now = new Date().getTime();

            if (rqLen > 0) {
                for (var i = 0; i < rqLen; i++) {
                    tasks.remove(removeQueue[i]);
                }
                removeQueue = [];
                if (tasks.length < 1) {
                    stopThread();
                    return;
                }
            }
            for (var i = 0, t, itime, rt, len = tasks.length; i < len; ++i) {
                t = tasks[i];
                itime = now - t.taskRunTime;
                if (t.interval <= itime) {
                    rt = t.run.apply(t.scope || t, t.args || [++t.taskRunCount]);
                    t.taskRunTime = now;
                    if (rt === false || t.taskRunCount === t.repeat) {
                        removeTask(t);
                        return;
                    }
                }
                if (t.duration && t.duration <= (now - t.taskStartTime)) {
                    removeTask(t);
                }
            }
        };

    this.start = function (task) {
        tasks.push(task);
        task.taskStartTime = new Date().getTime();
        task.taskRunTime = 0;
        task.taskRunCount = 0;
        startThread();
        return task;
    };


    this.stop = function (task) {
        removeTask(task);
        return task;
    };


    this.stopAll = function () {
        stopThread();
        for (var i = 0, len = tasks.length; i < len; i++) {
            if (tasks[i].onStop) {
                tasks[i].onStop();
            }
        }
        tasks = [];
        removeQueue = [];
    };

    this.hasRunning = function () { return tasks.length > 0 }
};


var maps, playForm, lTPoints_I = 0, zoom = 3, interval = 2000, stepLength = 1, quickness = 2, isPlay = true, playBtn, playRate = 1, runFn;



var runner = new Ext.util.TaskRunner();


//{ text: '快退', handler: quickBack },
//{ text: '播放', handler: PlayFn },
//{ text: '停止', handler: playStop },
//{ text: '快进', handler: quickPlay }

function updateStepLength() {

    stepLength = parseInt(playForm.get('stepLength').getValue())
}


function updatePlayRate(v) {
    playRate = parseInt(v, 10)

    if (!runner.hasRunning()) return;
    playForm.get('ivalue2').setValue(v)
    try {

        if (playRate) {
            runner.stopAll();
            if (runFn == lTPoints_IAdd) {
                runner.start({
                    run: runFn,
                    interval: interval / playRate
                });
            } else
                runner.start({
                    run: runFn,
                    interval: interval / playRate / quickness
                });
        } else
            playRate = 1;
    } catch (e) {

        dump(e)
    }
}
function playStop() {
    runner.stopAll();
    lTPoints_I = 0
    playForm.get('playProgress').setValue(lTPoints_I)
}

function quickBack() {
    runFn = lTPoints_ISub
    runner.stopAll();
    runner.start({
        run: runFn,
        interval: parseInt(interval / playRate / quickness)
    });

}
function quickPlay() {
    var tinterval = parseInt(interval / playRate / quickness)

    runFn = lTPoints_IAdd
    runner.stopAll();
    runner.start({
        run: runFn,
        interval: tinterval//parseInt(interval / playRate / quickness)
    });

}
function lTPoints_IAdd() {

    if (lTPoints_I >= lTPoints.length) {
        runner.stopAll()
        alert('已经到达终点');
        lTPoints_I = lTPoints.length - 1;
        maps.moveToCenter(lTPoints[lTPoints_I], zoom);
        return;
    }
    playForm.get('playProgress').setValue(lTPoints_I)
    maps.moveToCenter(lTPoints[lTPoints_I], zoom);
    lTPoints_I += stepLength

}
function lTPoints_ISub() {

    if (lTPoints_I < 0) {
        runner.stopAll()
        alert('已经到达出发点');
        lTPoints_I = 0;
        maps.moveToCenter(lTPoints[lTPoints_I], zoom);
        return;
    }
    playForm.get('playProgress').setValue(lTPoints_I)
    maps.moveToCenter(lTPoints[lTPoints_I], zoom);
    lTPoints_I -= stepLength

}

function lTPointsPlayer() {
    playForm.get('ivalue').setValue(lTPoints_I)

    if (lTPoints_I >= lTPoints.length) {
        runner.stopAll()
        alert('已经到达终点');
        lTPoints_I = lTPoints.length - 1;
        maps.moveToCenter(lTPoints[lTPoints_I], zoom);
        return;
    }
    playForm.get('playProgress').setValue(lTPoints_I)
    maps.moveToCenter(lTPoints[lTPoints_I], zoom);
    lTPoints_I += stepLength
}
function dragplay(v) {
    lTPoints_I = parseInt(v);

    maps.moveToCenter(lTPoints[lTPoints_I], zoom);

}
function PlayFn() {
    runFn = lTPointsPlayer
    if (isPlay) {
        var tinterval = parseInt(interval / playRate)

        isPlay = false;
        runner.start({
            run: runFn,
            interval: tinterval
        });
        playBtn.setText('Pause');
    } else {
        isPlay = true;
        runner.stopAll();
        playBtn.setText('Play');
    }
}





             

function dump(o) {
    var msg = "";
    for (ov in o) {
        msg += "\no[" + ov + "]" + o[ov];
    }
    alert(msg);
    return msg;
}
var marker;
var icon = new LTIcon('ext/ext-3.4.1/examples/button/images/add16.gif', [24, 24], [12, 12])
var pointDatas = [[11631376, 3999776], [11642688, 3982608], [11649198, 3996197], [11636224, 3991040]
    , [11637940, 3983135], [11646455, 3983399], [11648720, 3986983], [11648857, 3990513]

    , [11648925, 3993357], [11648375, 3995726], [11643637, 3998725], [11637181, 3998672]

    , [11630040, 3998409], [11627773, 3996883], [11627429, 3989617], [11629077, 3983028]
];
var lTPoints = [];

function toLTPoints(datas) {
    for (var p = 0; p < pointDatas.length; p++) {

        lTPoints.push(new LTPoint(pointDatas[p][0], pointDatas[p][1]));
    }

}
toLTPoints(pointDatas)
function addMarkermap(map) {

    if (marker) {

        map.overlayManager.removeOverLaysByType("marker");

    }

    //创建参数对象 

    var option = new LTMarkerOptions();

    //设置坐标 

    option.point = new LTPoint(11636224, 3991040);
    maps.centerAndZoom(new LTPoint(11631376, 3999776), 13);
    //创建点覆盖物 

    marker = new LTMarkerOverlay(option);

    //将覆盖物添加到地图，并纳入覆盖物管理类，来进统一管理 

    map.overlayManager.addOverLay(marker);



}

/* 

* 添加线覆盖物到地图 

*/
var polyLine;
function addLTLineOverlay(map) {

    if (true) {

        //创建参数对象 

        var option = new LTLineOptions();

        //生成折线的点数组 


        var markoption = new LTMarkerOptions();
        option.points = []
        //设置坐标 
        for (var p = 0; p < lTPoints.length; p++) {

            markoption.point = lTPoints[p];
            option.points.push(markoption.point);
            //创建点覆盖物 

            marker = new LTMarkerOverlay(markoption);

            marker.setIcon(icon);

            //将覆盖物添加到地图，并纳入覆盖物管理类，来进统一管理 

            map.overlayManager.addOverLay(marker);
        }


        //根据点数组创建一条折线 

        polyLine = new LTLineOverlay(option);



        map.overlayManager.addOverLay(polyLine);
      //  map.moveToCenter(lTPoints[0], 1);

    }

}